package com.hz.monitoring.module.hardware.convert.sensor_tenant_resource;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.common.util.json.JsonUtils;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource.vo.SensorTenantResourceCreateReqVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource.vo.SensorTenantResourceExcelVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource.vo.SensorTenantResourceRespVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource.vo.SensorTenantResourceUpdateReqVO;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomFields;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomSensorAttribute;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomWarningAttribute;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_tenant_resource.SensorTenantResourceDO;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 传感器租户配置 Convert
 *
 * @author 芋道源码
 */

public class SensorTenantResourceConvert {
    public static SensorTenantResourceDO convert(SensorTenantResourceCreateReqVO bean) {
        if (bean == null) {
            return null;
        }
        String attributeMetadata = null;
        if (Objects.nonNull(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.toJsonString(bean.getAttributeMetadata());
        }

        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }

        String warningMetadata = null;
        if (Objects.nonNull(bean.getWarningMetadata())) {
            warningMetadata = JsonUtils.toJsonString(bean.getWarningMetadata());
        }

        SensorTenantResourceDO sensorTenantResourceDO =
                SensorTenantResourceDO.builder()
                        .mcuTenantResourceId(bean.getMcuTenantResourceId())
                        .mcuModel(bean.getMcuModel())
                        .mcuChannel(bean.getMcuChannel())
                        .name(bean.getName())
                        .model(bean.getModel())
                        .remark(bean.getRemark())
                        .attributeMetadata(attributeMetadata)
                        .valueMetadata(valueMetadata)
                        .warningMetadata(warningMetadata)
                        .build();
        sensorTenantResourceDO.setTenantId(bean.getTenantId());
        return sensorTenantResourceDO;
    }

    public static SensorTenantResourceDO convert(SensorTenantResourceUpdateReqVO bean) {
        if (bean == null) {
            return null;
        }
        String attributeMetadata = null;
        if (Objects.nonNull(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.toJsonString(bean.getAttributeMetadata());
        }

        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }

        String warningMetadata = null;
        if (Objects.nonNull(bean.getWarningMetadata())) {
            warningMetadata = JsonUtils.toJsonString(bean.getWarningMetadata());
        }

        SensorTenantResourceDO sensorTenantResourceDO =
                SensorTenantResourceDO.builder()
                        .mcuTenantResourceId(bean.getMcuTenantResourceId())
                        .mcuChannel(bean.getMcuChannel())
                        .mcuModel(bean.getMcuModel())
                        .name(bean.getName())
                        .model(bean.getModel())
                        .remark(bean.getRemark())
                        .attributeMetadata(attributeMetadata)
                        .valueMetadata(valueMetadata)
                        .warningMetadata(warningMetadata)
                        .build();
        sensorTenantResourceDO.setId(bean.getId());
        sensorTenantResourceDO.setTenantId(bean.getTenantId());
        return sensorTenantResourceDO;
    }

    public static SensorTenantResourceRespVO convert(SensorTenantResourceDO bean) {
        if (bean == null) {
            return null;
        }

        List<CustomSensorAttribute> attributeMetadata = new ArrayList<>();
        if (StringUtils.hasLength(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.parseArray(bean.getAttributeMetadata(), CustomSensorAttribute.class).stream().sorted(Comparator.comparing(CustomSensorAttribute::getSorts)).collect(Collectors.toList());
        }
        List<CustomFields> valueMetadata = new ArrayList<>();
        if (StringUtils.hasLength(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.parseArray(bean.getValueMetadata(), CustomFields.class).stream().sorted(Comparator.comparing(CustomFields::getSorts)).collect(Collectors.toList());
        }

        List<CustomWarningAttribute> warningMetadata =  new ArrayList<>();
        if (StringUtils.hasLength(bean.getWarningMetadata())) {
            warningMetadata = JsonUtils.parseArray(bean.getWarningMetadata(), CustomWarningAttribute.class);
        }
        SensorTenantResourceRespVO sensorTenantResourceRespVO = new SensorTenantResourceRespVO();
        sensorTenantResourceRespVO.setMcuTenantResourceId(bean.getMcuTenantResourceId());
        sensorTenantResourceRespVO.setMcuModel(bean.getMcuModel());
        sensorTenantResourceRespVO.setMcuChannel(bean.getMcuChannel());
        sensorTenantResourceRespVO.setName(bean.getName());
        sensorTenantResourceRespVO.setModel(bean.getModel());
        sensorTenantResourceRespVO.setRemark(bean.getRemark());
        sensorTenantResourceRespVO.setAttributeMetadata(attributeMetadata);
        sensorTenantResourceRespVO.setValueMetadata(valueMetadata);
        sensorTenantResourceRespVO.setWarningMetadata(warningMetadata);
        sensorTenantResourceRespVO.setId(bean.getId());
        sensorTenantResourceRespVO.setCreateTime(bean.getCreateTime());
        sensorTenantResourceRespVO.setTenantId(bean.getTenantId());
        return sensorTenantResourceRespVO;
    }

    public static List<SensorTenantResourceRespVO> convertList(List<SensorTenantResourceDO> list) {
        if (list == null) {
            return null;
        }
        List<SensorTenantResourceRespVO> list1 = new ArrayList<SensorTenantResourceRespVO>(list.size());
        for (SensorTenantResourceDO sensorTenantResourceDO : list) {
            list1.add(convert(sensorTenantResourceDO));
        }
        return list1;
    }


    public static PageResult<SensorTenantResourceRespVO> convertPage(PageResult<SensorTenantResourceDO> page) {
        if (page == null) {
            return null;
        }

        PageResult<SensorTenantResourceRespVO> pageResult = new PageResult<SensorTenantResourceRespVO>();

        pageResult.setList(convertList(page.getList()));
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

    public static List<SensorTenantResourceExcelVO> convertList02(List<SensorTenantResourceDO> list) {
        if (list == null) {
            return null;
        }

        List<SensorTenantResourceExcelVO> list1 = new ArrayList<SensorTenantResourceExcelVO>(list.size());
        for (SensorTenantResourceDO sensorTenantResourceDO : list) {
            list1.add(sensorTenantResourceDOToSensorTenantResourceExcelVO(sensorTenantResourceDO));
        }

        return list1;
    }

    protected static SensorTenantResourceExcelVO sensorTenantResourceDOToSensorTenantResourceExcelVO(SensorTenantResourceDO bean) {
        if (bean == null) {
            return null;
        }
        SensorTenantResourceExcelVO sensorTenantResourceExcelVO = new SensorTenantResourceExcelVO();
        sensorTenantResourceExcelVO.setMcuTenantResourceId(bean.getMcuTenantResourceId());
        sensorTenantResourceExcelVO.setMcuModel(bean.getMcuModel());
        sensorTenantResourceExcelVO.setMcuChannel(bean.getMcuChannel());
        sensorTenantResourceExcelVO.setName(bean.getName());
        sensorTenantResourceExcelVO.setModel(bean.getModel());
        sensorTenantResourceExcelVO.setRemark(bean.getRemark());
        sensorTenantResourceExcelVO.setCreateTime(bean.getCreateTime());
        return sensorTenantResourceExcelVO;
    }

}
